package org.celstec.mobilegame.locatory;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.ForeignKey;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.celstec.mobilegame.gamaray.Dimension;
import org.celstec.mobilegame.server.PMF;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	public Key key;
	
	@Persistent
	public int score;

	@Persistent
	public String uid;

	@Persistent
	public double lat;
	
	@Persistent
	public double lon;
	
	@Persistent(defaultFetchGroup = "true")
	@ForeignKey(table = "org.celstec.mobilegame.locatory.CardLocation")
	public Key previousClick;

	@Persistent(defaultFetchGroup = "true")
	@ForeignKey(table = "org.celstec.mobilegame.locatory.LocatoryPlay")
	public Key play; // todo make play

//	@Persistent(defaultFetchGroup = "true")
//	@ForeignKey(table = "org.celstec.mobilegame.locatory.Card")
//	public Set<Key> cardBag = new HashSet();
	
	@Persistent
	public Set<String> cardBagString = new HashSet();
	
	private User() {
		score = 0;
//		addCardToBag(getRamdomCard());
//		cardBag = new HashSet();
	}
	private User(String uid){
		this();
		this.uid = uid;
		
	}
	public void initNotPersistentObjects(PersistenceManager pm){
		if (cardBagString== null) cardBagString = new HashSet<String>();
	}
	
	public double getLat() {
		return lat;
	}
	public void setLat(double lat) {
		this.lat = lat;
	}
	public CardLocation getPreviousClick() {
		if (previousClick == null) return null;
		return CardLocation.getObject(previousClick);
	}
	public void setPreviousClick(CardLocation previousClick) {
		if (previousClick == null) this.previousClick = null;
		else	this.previousClick = previousClick.id;
	}
	public double getLon() {
		return lon;
	}
	public void setLon(double lon) {
		this.lon = lon;
	}
public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	//	public Set<Key> getCardBag() {
//		return cardBag;
//	}
//	public void setCardBag(Set<Key> cardBag) {
//		this.cardBag = cardBag;
//	}
	public void addScore(int amount) {
		score += amount;
	}

	public void addCardToBag(Card c){
		if (cardBagString==null) cardBagString = new HashSet();
		if (c==null) System.out.println("2 null");
		if (cardBagString!= null && c !=null)		cardBagString.add(c.cardId);
	}
	
	public void removeCardFromBag(Card c){
		cardBagString.remove(c.cardId);
	}
	
	public void removeCardFromBag(String c){
		cardBagString.remove(c);
	}
//	public void storeItem(String itemName, Object item) {
//		hm.put(itemName, item);
//	}
//
//	public Object getItem(String itemName) {
//		return hm.get(itemName);
//	}
	
	public static User getUser(String uid) {

		PersistenceManager pm = PMF.get().getPersistenceManager();
		User returnUser = null;
		try {
			String query = "select from " + User.class.getName()
					+ " where uid =='" + uid + "'";
			List<User> user = (List<User>) pm.newQuery(query).execute();
			if (user.size() != 0) {
				returnUser = user.get(0);
			} 
		} finally {
			pm.close();

		}
		return returnUser;
	}
	
//	public static Card getRamdomCard() {
//
//		PersistenceManager pm = PMF.get().getPersistenceManager();
//		Card returnUser = null;
//		try {
//			String query = "select from " + Card.class.getName();
//			List<Card> user = (List<Card>) pm.newQuery(query).execute();
//			if (user.size() != 0) {
//				returnUser = user.get(0);
//			} 
//		} finally {
//			pm.close();
//
//		}
//		return returnUser;
//	}
//	
	public static void storeUser(String uid, double lat, double lon, Key play){
		User u = new User(uid);
		u.lat = lat;
		u.lon = lon;
		u.play = play;
//		Card c = getRamdomCard();
////		u.addCardToBag(c);
//		u.cardBagString.add(c.key.toString());
//		u.cardBag.add(c.key);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(u);
//			u.setCardBag(new HashSet());
		} finally {			
			pm.close();
		}
	}

}
